package cn.wangzg.mymusic;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

class ViewHolder {
    TextView tv_title;
    TextView tv_artist;
    TextView tv_duration;
    ImageView tv_album;

    View itemView;

    public ViewHolder(View itemView) {
        if (itemView == null) {
            throw new IllegalArgumentException("item View can not be null!");
        }
        this.itemView = itemView;
        tv_title = itemView.findViewById(R.id.tv_title);
        tv_artist = itemView.findViewById(R.id.tv_artist);
        tv_duration = itemView.findViewById(R.id.tv_duration);
        tv_album = itemView.findViewById(R.id.tv_album);
    }
}

public class ListAdapter extends BaseAdapter {
    private List<Music> musicList;
    private LayoutInflater layoutInflater;
    private Context context;
    private int currentPos = -1;
    private ViewHolder holder = null;

    public ListAdapter(Context context,List<Music> musicList) {
        this.musicList = musicList;
        this.context = context;

        layoutInflater = LayoutInflater.from(context);
    }

    public void setFocusItemPos(int pos){
        currentPos = pos;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return musicList.size();
    }

    @Override
    public Object getItem(int position) {
        return musicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = layoutInflater.inflate(R.layout.music_item,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_title.setText(musicList.get(position).getTitle());
        holder.tv_artist.setText(musicList.get(position).getArtist());
        holder.tv_duration.setText(MusicUtils.formatTime(musicList.get(position).getDuration()));
        Bitmap bitmap = getImage(musicList.get(position).getAlbumId());
        if (bitmap != null){
            holder.tv_album.setImageBitmap(bitmap);
        }
        return convertView;
    }

    private Bitmap getImage(int id) {
        int album_id = id;
        String albumArt = getAlbumArt(album_id);
        Bitmap bm = null;
        if (albumArt != null) {
            bm = BitmapFactory.decodeFile(albumArt);
            return bm;
        }
        return null;
    }

    private String getAlbumArt(int album_id) {
        String mUriAlbums = "content://media/external/audio/albums";
        String[] projection = new String[] { "album_art" };
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cur = contentResolver.query( Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)), projection, null, null, null);
        String album_art = null;
        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            album_art = cur.getString(0);
        }
        cur.close();
        cur = null;
        return album_art;
    }
}
